package com.xmsx.back.config;

import com.xmsx.back.mapper.index.RecruitmentMapper;
import com.xmsx.back.pojo.Recruitment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;

@Configuration
@EnableScheduling
public class SpiderConfig {

    @Autowired
    RecruitmentMapper recruitmentMapper;

    private static String url = "http://shikong.sdu.edu.cn/jyxx.htm";


    /**
     * 配置定时爬虫任务 指定定时任务间隔1小时
     */
    @Scheduled(fixedRate = 3600000)
    private void configureSpider() {
        System.out.println("执行招聘信息爬虫任务时间: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Document doc = null;
        int recruitmentCount = 0;

        try {
            doc = Jsoup.connect(url).timeout(5000).get();
            String recruitmentNum = doc.getElementsByAttributeValue("class", "p_t").first().text();
            recruitmentNum = recruitmentNum.replace("共", "");
            recruitmentNum = recruitmentNum.replace("条", "");
            recruitmentCount = Integer.parseInt(recruitmentNum);

            int pageNum = (int) Math.ceil((double) recruitmentCount / 10);

            Elements recruitments = doc.getElementsByAttributeValue("class", "field-content");
            for (int i = 0; i < recruitments.size(); i++) {
                Recruitment rec = new Recruitment();
                Element e = recruitments.get(i);
                if (!e.children().attr("href").startsWith("http")) {
                    if (e.children().attr("href").startsWith("../"))
                        rec.setWebsite("http://shikong.sdu.edu.cn/" + e.children().attr("href").replace("../", ""));
                    else
                        rec.setWebsite("http://shikong.sdu.edu.cn/" + e.children().attr("href"));
                } else
                    rec.setWebsite(e.children().attr("href"));

                Recruitment queryIfExist = recruitmentMapper.getByWebsite(rec.getWebsite());
                if (queryIfExist != null) {
                    System.out.println("爬虫停止到：" + queryIfExist);
                    return;
                }

                rec.setTitle(e.children().attr("title"));
                i++;
                e = recruitments.get(i);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date d = null;

                d = format.parse(e.text().replace("[", "").replace("]", ""));

                rec.setDatetime(new Date(d.getTime()));

                String[] temp = rec.getTitle().split("】");
                HashSet<String> result = new HashSet<>();
                for (int x = 0; x < temp.length - 1; x++) {
                    temp[x] = temp[x].replace("【", "");
                    if (temp[x].indexOf("/") != -1) {
                        String[] str = temp[x].split("/");
                        for (int y = 0; y < str.length; y++)
                            result.add(str[y]);
                        continue;
                    } else {
                        result.add(temp[x]);
                    }
                }
                rec.setTitle(temp[temp.length - 1]);
                String tagArray[] = new String[0];
                tagArray = result.toArray(tagArray);
                String tag = "";

                for (int y = 0; y < tagArray.length; y++) {
                    recruitmentMapper.setTag(tagArray[y], rec.getWebsite());
                    if (y == 0)
                        tag += tagArray[y];
                    else
                        tag += "," + tagArray[y];
                }
                rec.setTags(tag);
                recruitmentMapper.setJob(rec.getWebsite(), rec.getTitle(), rec.getDatetime(), rec.getTags());
            }
            for (int z = pageNum - 1; z > 0; z--) {
                url = "http://shikong.sdu.edu.cn/jyxx/" + z + ".htm";
                doc = Jsoup.connect(url).timeout(5000).get();
                recruitments = doc.getElementsByAttributeValue("class", "field-content");
                for (int i = 0; i < recruitments.size(); i++) {
                    Recruitment rec = new Recruitment();
                    Element e = recruitments.get(i);
                    if (!e.children().attr("href").startsWith("http")) {
                        if (e.children().attr("href").startsWith("../"))
                            rec.setWebsite("http://shikong.sdu.edu.cn/" + e.children().attr("href").replace("../", ""));
                        else
                            rec.setWebsite("http://shikong.sdu.edu.cn/" + e.children().attr("href"));
                    } else
                        rec.setWebsite(e.children().attr("href"));

                    Recruitment queryIfExist = recruitmentMapper.getByWebsite(rec.getWebsite());
                    if (queryIfExist != null) {
                        System.out.println("爬虫停止到：" + queryIfExist);
                        return;
                    }

                    rec.setTitle(e.children().attr("title"));
                    i++;
                    e = recruitments.get(i);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    java.util.Date d = null;

                    d = format.parse(e.text().replace("[", "").replace("]", ""));

                    rec.setDatetime(new Date(d.getTime()));

                    String[] temp = rec.getTitle().split("】");
                    HashSet<String> result = new HashSet<>();
                    for (int x = 0; x < temp.length - 1; x++) {
                        temp[x] = temp[x].replace("【", "");
                        if (temp[x].indexOf("/") != -1) {
                            String[] str = temp[x].split("/");
                            for (int y = 0; y < str.length; y++)
                                result.add(str[y]);
                            continue;
                        } else {
                            result.add(temp[x]);
                        }
                    }
                    rec.setTitle(temp[temp.length - 1]);
                    String tagArray[] = new String[0];
                    tagArray = result.toArray(tagArray);
                    String tag = "";
                    for (int y = 0; y < tagArray.length; y++) {
                        recruitmentMapper.setTag(tagArray[y], rec.getWebsite());
                        if (y == 0)
                            tag += tagArray[y];
                        else
                            tag += "," + tagArray[y];
                    }
                    rec.setTags(tag);
                    recruitmentMapper.setJob(rec.getWebsite(), rec.getTitle(), rec.getDatetime(), rec.getTags());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
